﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
namespace NodeCanvas{
public static class ArrayUtility
{
	public static T[] Add<T>(T[] array, T item)
	{
		return (new List<T>(array)
			{
				item
			}).ToArray();
	}

	public static T[] AddRange<T>(T[] array, T[] items)
	{
		List<T> ts = new List<T>(array);
		ts.AddRange(items);
		return ts.ToArray();
	}

	public static T[] Copy<T>(T[] array)
	{
		return (new List<T>(array)).ToArray();
	}

	public static T[] MoveItem<T>(T[] array, int oldIndex, int newIndex)
	{
		List<T> ts = new List<T>(array);
		T item = ts[oldIndex];
		ts.RemoveAt(oldIndex);
		ts.Insert(newIndex, item);
		return ts.ToArray();
	}

	public static T[] Remove<T>(T[] array, T item)
	{
		List<T> ts = new List<T>(array);
		ts.Remove(item);
		return ts.ToArray();
	}

	public static T[] RemoveAt<T>(T[] array, int index)
	{
		List<T> ts = new List<T>(array);
		ts.RemoveAt (index);
		return ts.ToArray();
	}

	public static T[] Insert<T>(T[] array, T item, int index)
	{
		List<T> ts = new List<T>(array);
		ts.Insert (index,item);
		return ts.ToArray();
	}


	public static T[] Sort<T>(T[] array)
	{
		List<T> ts = new List<T>(array);
		ts.Sort();
		return ts.ToArray();
	}

	public static T[] Sort<T>(T[] array,IComparer<T> comparer)
	{
		List<T> ts = new List<T> (array);
		ts.Sort(comparer);
		return ts.ToArray();
	}

	public static T[] Reverse<T>(T[] array)
	{
		List<T> ts = new List<T>(array);
		ts.Reverse ();
		return ts.ToArray();
	}
}

	public static class AssetsFind{
		public static T[] Find<T>(GameObject[] objects){
			List<T> list = new List<T> ();
			if (objects == null)
				return null;
			for (int i = 0; i < objects.Length; i++) {
				if (objects [i] != null) {
					T[] childT = objects [i].GetComponentsInChildren<T> (true);
					for (int x = 0; x < childT.Length; x++) {
						list.Add (childT [x]);
					}
				}
			}
			return list.ToArray ();
		}

		public static GameObject[] FindAllPrefab(){
			string[] dircts = GetFiles ("*.prefab");
			GameObject[] objects =new GameObject[dircts.Length];
			dircts = filter (dircts);
			for (int i = 0; i < dircts.Length; i++) {
				objects [i] = AssetDatabase.LoadMainAssetAtPath (dircts [i]) as GameObject;
				if (EditorUtility.DisplayCancelableProgressBar ("Find Prefabs", i.ToString (), (float)i / dircts.Length)) {
					EditorUtility.ClearProgressBar ();
					return null;
				}
			}
			EditorUtility.ClearProgressBar ();
			return objects;
		}
		public static string[] GetFiles(string e){
			string[] dircts = Directory.GetFiles (Application.dataPath, e, SearchOption.AllDirectories);
			return dircts;
		}
		public static string[] filter(string[] path){
			for (int i = 0; i < path.Length; i++) {
				path [i] = path [i].Replace (Application.dataPath, "Assets");
			}
			return path;
		 }
	}
}